package com.topboya.officeuser.service.serviceImpl;

import com.topboya.officeuser.entity.BoyaSalesInfo;
import com.topboya.officeuser.entity.Office;
import com.topboya.officeuser.entity.User;
import com.topboya.officeuser.mapper.OfficeMapper;
import com.topboya.officeuser.mapper.UserMapper;
import com.topboya.officeuser.service.OfficeService;
import com.topboya.officeuser.utils.IfOffice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @className OfficeServiceImpl
 * @Author xiaxin
 * @Date 2020/8/8 18:37
 * @Version 1.0
 **/
@Service
public class OfficeServiceImpl implements OfficeService {
    @Autowired
    OfficeMapper officeMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public int insert(Office office) {
        IfOffice.ifOffice(office);

        int i = officeMapper.insert(office);
        return i;
    }


    @Override
    public int update(Office office) {
        IfOffice.ifOffice(office);
        return officeMapper.update(office);
    }

    @Transactional
    @Override
    public int deleteOfficeAndUser(String id) {
        int i = userMapper.deleteUser(id);
        int j = officeMapper.deleteOffice(id);

        return j;
    }

    @Override
    public Office select(String id) {
        return officeMapper.select(id);
    }

    @Override
    public List<Office> selectAllPageAndIf(Map<String, Object> mapOffice,Office office) {
        if(!StringUtils.isEmpty(office.getId())) {
            mapOffice.put("id", office.getId());
        }
        if(!StringUtils.isEmpty(office.getParentId())) {
            mapOffice.put("parentId", office.getParentId());
        }
        if(!StringUtils.isEmpty(office.getParentIds())) {
            mapOffice.put("parentIds", office.getParentIds());
        }
        if(!StringUtils.isEmpty(office.getName())) {
            mapOffice.put("name", office.getName());
        }
        if(!StringUtils.isEmpty(office.getSort())) {
            mapOffice.put("sort", office.getSort());
        }
        if(!StringUtils.isEmpty(office.getType())) {
            mapOffice.put("type", office.getType());
        }
        if(!StringUtils.isEmpty(office.getMaster1())) {
            mapOffice.put("master1", office.getMaster1());
        }
        if(!StringUtils.isEmpty(office.getMaster2())) {
            mapOffice.put("master2", office.getMaster2());
        }
        if(!StringUtils.isEmpty(office.getCreateBy())) {
            mapOffice.put("createBy", office.getCreateBy());
        }
        if(!StringUtils.isEmpty(office.getCreateDate())) {
            mapOffice.put("createDate", office.getCreateDate());
        }
        if(!StringUtils.isEmpty(office.getUpdateBy())) {
            mapOffice.put("updateBy", office.getUpdateBy());
        }
        if(!StringUtils.isEmpty(office.getRemarks())) {
            mapOffice.put("remarks", office.getRemarks());
        }
        if(!StringUtils.isEmpty(office.getDelFlag())) {
            mapOffice.put("delFlag", office.getDelFlag());
        }
        return officeMapper.selectAllPageAndIf(mapOffice);
    }

    @Override
    public List<Office> getAllOfficeAndUserByTree() {
        List<Office> officeList = officeMapper.queryAll(null);
        List<User>  userList = userMapper.queryAll(null);
        List<Office> resultList = buildOffice(officeList,userList);
        return resultList;
    }

    @Override
    public void insertBoyaSalesInfoBatch(List<BoyaSalesInfo> boyaSalesInfoList) {
        officeMapper.insertSalesInforBatch(boyaSalesInfoList);
    }

    private List<Office> buildOffice(List<Office> officeList,List<User> userList) {

        List<Office> finalOfficeList = new ArrayList<>();

        for(Office officeNode : officeList){
            if("0".equals(officeNode.getParentId())){
                officeNode.setSort(1d);
                finalOfficeList.add(selectChildren(officeNode,officeList,userList));
            }
        }
        return finalOfficeList;
    }

    private Office selectChildren(Office officeNode, List<Office> officeList,List<User> userList) {

        officeNode.setOfficeChild(new ArrayList<Office>());

        for(Office it : officeList){

            if(officeNode.getId().equals(it.getParentId())){
                User user = new User();
                user.setOfficeId(officeNode.getId());
                List<User> users = userMapper.queryAll(user);
                double sort = officeNode.getSort()+1;
                it.setSort(sort);
                officeNode.setUserChild(users);
                officeNode.getOfficeChild().add(selectChildren(it,officeList,userList));
            }

        }

        return officeNode;
    }

}
